var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isbumen:0,  //0关闭,1添加部门,2修改部门
    ismore_mana:false//更多管理
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    wx.setNavigationBarTitle({
      title:options.part_name
    });
    if(options.id){
      var layer_list=JSON.parse(options.layer_list);
      layer_list.push(options.part_name);
      this.setData({
        client_logo:app.globalData.loginInfo.logo!=0?app.globalData.loginInfo.logo:'http://www.xiaobangcai.com/upload/smallprogram/client_def.png',
      gname:app.globalData.loginInfo.client_abbre,
        id:options.id,   //当前部门id
        lay_num:parseInt(options.lay_num),   //当前层级
        cur_part_name:options.part_name,  //当前部门名称
        layer_list:layer_list    //层级表
      });
    };
  },
  // 页面刷新
  loadlist:function(){
    var that = this;
    app.AppHttp('api/Client/searchChildrenPart', {
      id:that.data.id
    }, function (res) {
      console.log(JSON.stringify(res.data));
      that.setData({
        children_part:res.data.part_list,
        person_list:res.data.person_list,
        len:res.data.part_list.length+res.data.person_list.length,    
      });   
    });
  },
  noneEnoughPeople:function(){
    return;
  },
  // 添加部门
  add_bumen:function(){
    this.setData({
      isbumen:1,
      part_name:''
    });
  },
  // 关闭部门
  cancel_add_bumen:function(){
    this.setData({
      isbumen:0
    });
  },
  // 输入部门名称
  bumen_input:function(e){
    this.setData({
      part_name:e.detail.value
    });
  },
  // 确定添加部门
  conf_add_bumen:function(){
    var that = this;
    if(that.data.part_name==''){
      wx.showLoading({
        title:'输入为空',
        duration:500
      });
      return;
    };
    if(that.data.isbumen==1){ //添加子部门
      app.AppHttp('api/Client/addDepartMent', {
        client_id:app.globalData.loginInfo.id,
        part_name:that.data.part_name,
        depart_id:that.data.id
      }, function (res) {
        console.log(JSON.stringify(res.data));
        that.setData({
          isbumen:0
        });
        that.loadlist();
      });
    }else if(that.data.isbumen==2){  //修改部门
      app.AppHttp('api/Client/updatePartNmae', {
        part_name:that.data.part_name,
        id:that.data.id
      }, function (res) {
        console.log(JSON.stringify(res.data));
        wx.setNavigationBarTitle({
          title:that.data.part_name
        });
        that.setData({
          isbumen:0,
          ismore_mana:false,
          cur_part_name:that.data.part_name,
          ["layer_list[" + that.data.lay_num + "]"]:that.data.part_name
        });
      });
    };      
  },
  // 删除子部门
  del_part_but:function(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除此部门？',
      success: function (sm) {
        if (sm.confirm) {
          app.AppHttp('api/Client/deletePart', {
            id:that.data.id
          }, function (res) {
            console.log(JSON.stringify(res.data));
            wx.navigateBack({
              delta:1
            });
          });
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    }); 
  },
  // 打开更多管理
  open_mana:function(){
    this.setData({
      ismore_mana:true
    });
  },
  // 关闭更多管理
  close_mana:function(){
    this.setData({
      ismore_mana:false
    });
  },
  // 修改当前部门名称
  xiu_bumen:function(){
    this.setData({
      isbumen:2,
      part_name:this.data.cur_part_name
    });
  },
  // 去设置主管
  set_zhuguan:function(){
    wx.navigateTo({
      url:"../check_depart/check_depart?type="+1+'&check_name='+'设置主管'+'&lay_num='+this.data.lay_num+'&id='+this.data.id+'&page_num='+this.data.lay_num
    });
  },
  // 批量移动成员
  batch_move:function(){ 
    wx.setStorageSync('children_part_ids',[]) //存 
    wx.setStorageSync('person_ids',[]) //存 
    wx.setStorageSync('children_obj',{}) //存 
    wx.setStorageSync('person_obj',{}) //存 
    wx.setStorageSync('all_index',[]) //存 
    if(this.data.person_list.length==0 && this.data.children_part.length==0){  //该部门为空，进入根目录
      wx.navigateTo({
        url:"../check_depart/check_depart?type="+2+'&check_name='+'移动成员'+'&lay_num='+0+'&id='+0+'&page_num='+0
      });
    }else{   //否则进入当前目录
      wx.navigateTo({
        url:"../check_depart/check_depart?type="+2+'&check_name='+'移动成员'+'&lay_num='+this.data.lay_num+'&id='+this.data.id+'&page_num='+this.data.lay_num
      });
    }; 
  },
  // 查看用户
  itemClick: function (e) {
    var index = e.currentTarget.id;
    var yg = this.data.person_list[index];
    if (yg.is_validate == 1){
      wx.navigateTo({
        url: '../yuangongshezhi/yuangongshezhi?id='+yg.id+'&nick_name='+yg.nickname
      });    
    }
  },
  // 接受员工
  jieshouClick:function(event){
    var index = event.currentTarget.id;
    var yg = this.data.person_list[index];
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定接受？',
      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.url + 'api/Client/examineEmployee',
            method: "POST",
            data: {
              openid: app.globalData.openid,
              id: yg.id
            },
            success: function (res) {
              console.log(JSON.stringify(res.data));
              yg.is_validate = 1;
              that.setData({
                ["person_list[" + index + "]"]: yg
              });
            }
          });
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    });   
  },
  // 去下一级部门
  todepart:function(e){
    var index=e.currentTarget.dataset.index;
    var id=this.data.children_part[index].id;
    var part_name=this.data.children_part[index].part_name;
    var layer_list=this.data.layer_list;
    var lay_num=parseInt(this.data.lay_num)+1; 
    wx.navigateTo({
      url:'../depart/depart?id='+id+'&part_name='+part_name+'&layer_list='+JSON.stringify(layer_list)+'&lay_num='+lay_num
    });
  },
  // 点击层级
  type_layer:function(e){
    var back_lay=parseInt(this.data.lay_num)-parseInt(e.currentTarget.dataset.index);  //返回层级
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    console.log(back_lay);
    if(back_lay>0){
      wx.navigateBack({
        delta:back_lay
      })
    }else if(back_lay==0){  //当前层，仅刷新
      this.loadlist();
    };
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideShareMenu();//隐藏转发分享按钮
    this.setData({
      ismore_mana:false,
      canvasHidden: true,
      set_head_portrait:app.globalData.loginInfo.set_head_portrait,
      head_portrait: app.globalData.loginInfo.head_portrait
    });
    this.loadlist();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadlist();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    that.setData({
      canvasHidden: false
    });
    return {
      title: ' '+that.data.gname+'-邀请您加入',
      path: '/pages/wo/tuijian/tuijian?ShareOpenid=' + app.globalData.openid + '&type=' + 'invite'+'&part_id='+that.data.id
    }
  }
})